package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.vo.LikeCountVo;
import com.tanhua.model.vo.PageResult;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


@DubboService
public class UserLikeApiImpl implements UserLikeApi {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public Boolean saveOrUpdate(Long likeUserId, Long userId, boolean isLike) {
        try {
            Boolean flag = false;
            Query query = Query.query(Criteria.where("likeUserId").is(likeUserId).and("userId").is(userId));
            Query queryLike = Query.query(Criteria
                    .where("userId").is(likeUserId)
                    .and("likeUserId").is(userId)
                    .and("isLike").is(true));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            if (userLike == null) {
                userLike = new UserLike();
                userLike.setLikeUserId(likeUserId);
                userLike.setUserId(userId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                userLike.setUpdated(System.currentTimeMillis());

                if (mongoTemplate.exists(queryLike, UserLike.class)) {
                    userLike.setAlreadyLove(true);
                    Update update = Update.update("alreadyLove", true).set("updated", System.currentTimeMillis());
                    mongoTemplate.updateFirst(queryLike, update, UserLike.class);
                    flag = true;
                } else {
                    userLike.setAlreadyLove(false);
                }
                mongoTemplate.save(userLike);
            } else {
                Update update = Update.update("isLike", isLike).set("updated", System.currentTimeMillis());
                if (isLike) {
                    if (mongoTemplate.exists(queryLike, UserLike.class)) {
                        update.set("alreadyLove", true);
                        mongoTemplate.updateFirst(queryLike,
                                Update.update("alreadyLove", true)
                                        .set("updated", System.currentTimeMillis()), UserLike.class);
                        flag = true;
                    }
                } else {
                    if (mongoTemplate.exists(queryLike, UserLike.class)) {
                        mongoTemplate.updateFirst(queryLike,
                                Update.update("alreadyLove", false)
                                        .set("updated", System.currentTimeMillis()), UserLike.class);
                    }
                    update.set("alreadyLove", false);
                }
                mongoTemplate.updateFirst(query, update, UserLike.class);
            }
            return flag;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public PageResult<UserLike> together(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria
                .where("userId").is(userId)
                .and("isLike").is(true)
                .and("alreadyLove").is(true));
        long count = mongoTemplate.count(query, UserLike.class);
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("updated")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return new PageResult<UserLike>(page, pagesize, Math.toIntExact(count), userLikes);
    }


    @Override
    public PageResult<UserLike> findLikes(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria
                .where("userId").is(userId)
                .and("isLike").is(true)
                .and("alreadyLove").is(false));
        long count = mongoTemplate.count(query, UserLike.class);
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("updated")));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        return new PageResult<UserLike>(page, pagesize, Math.toIntExact(count), userLikes);
    }

    @Override
    public PageResult<UserLike> findFans(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria
                .where("likeUserId").is(userId)
                .and("isLike").is(true)
                .and("alreadyLove").is(false));
        long count = mongoTemplate.count(query, UserLike.class);
        query.skip((page - 1) * pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("updated")));
        List<UserLike> fans = mongoTemplate.find(query, UserLike.class);
        return new PageResult<UserLike>(page, pagesize, Math.toIntExact(count), fans);
    }

    @Override
    public LikeCountVo findCount(Long userId) {
        Query query = Query.query(Criteria
                .where("userId").is(userId)
                .and("isLike").is(true)
                .and("alreadyLove").is(true));
        long eachLoveCount = mongoTemplate.count(query, UserLike.class);
        query = Query.query(Criteria
                .where("userId").is(userId)
                .and("isLike").is(true)
                .and("alreadyLove").is(false));
        long loveCount = mongoTemplate.count(query, UserLike.class);
        query = Query.query(Criteria
                .where("likeUserId").is(userId)
                .and("isLike").is(true)
                .and("alreadyLove").is(false));
        long fanCount = mongoTemplate.count(query, UserLike.class);
        return new LikeCountVo(Math.toIntExact(eachLoveCount),Math.toIntExact(loveCount),Math.toIntExact(fanCount));
    }
}
